package org.linlinjava.litemall.admin.controller;

import javax.validation.constraints.NotNull;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.dto.MessageDto;
import org.linlinjava.litemall.admin.service.MessageService;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/message")
@Validated
public class AdminMessageController {

    /**
     * 消息服务
     */
    @Autowired
    private MessageService messageService;

    /**
     * 查询消息列表
     * 消息列表
     *
     * @param title 标题
     * @param type  类型
     * @param page  页面
     * @param limit 限制
     * @param sort  排序
     * @param order 订单
     * @return {@link Object}
     */
    @GetMapping("/queryMessageList")
    @LogAnno
    public Object queryMessageList(String title, Byte type,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer limit,
                                   @Sort @RequestParam(defaultValue = "add_time") String sort,
                                   @Order @RequestParam(defaultValue = "desc") String order) {
        return messageService.queryMessageList(title, type, page, limit, sort, order);
    }

    /**
     * 插入
     * 新增
     *
     * @param messageDto 消息dto
     * @return {@link Object}
     */
    @PostMapping("/create")
    @LogAnno
    public Object insert(@RequestBody MessageDto messageDto) {
        return messageService.insert(messageDto);
    }


    /**
     * 删除
     *
     * @param id id
     * @return {@link Object}
     */
    @DeleteMapping("/delete")
    @LogAnno
    public Object delete(@NotNull @RequestParam(value = "id") Integer id) {
        return messageService.delete(id);
    }

}
